/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.controller;

import com.vng.zing.survey.memcached.SurveyCache;
import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.Question;
import com.vng.zing.survey.thrift.model.SurveyItem;
import com.vng.zing.survey.thrift.model.SurveyItemResult;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vng.zing.survey.model.CommonModel;
import com.vng.zing.survey.model.RenderModel;
import com.vng.zing.survey.thrift.model.UserType;

/**
 *
 * @author namhh
 */
public class EditSurveyController extends BaseController {

    private static UserType userType = UserType.ADMIN;

    @Override
    public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {
        if (req.getParameter("id") == null) {
            return showError("Không tìm thấy khảo sát yêu cầu!");
        }

        int idSurvey = Integer.parseInt(req.getParameter("id"));
        String content = "";

        TemplateDataDictionary dic = TemplateDictionary.create();
        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Chỉnh sữa khảo sát"));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));
        Map mapUser = checkMember(req, userType);
        if (mapUser.containsValue(ErrorType.FAIL) || mapUser.containsValue(ErrorType.ERROR_SYSTEM)) {
            return CommonModel.getError(dic, (String) mapUser.get("msg"));
        }
        int idUser = (int) mapUser.get("msg");
        boolean isAdmin = (boolean) mapUser.get("admin");
         if(!isAdmin){
             return CommonModel.getError(dic, "Bạn không phải admin");
        
        }   
        SurveyClient surveyClient = SurveyClient.getInstance();
        SurveyItemResult itemResult = SurveyCache.getSurveyWithCache(idSurvey);
        if (itemResult.getErro() != com.vng.zing.survey.thrift.model.Error.SUCCESS || itemResult.getSurveyItem().getIDUser() != idUser) {
            content = CommonModel.getError(dic, "Khảo sát không tồn tại!");
            return content;

        }

        Map<Short, Integer> map = surveyClient.listQuesCanEdit(idSurvey);
        String responsed = surveyClient.getSurveyStats(idSurvey).getSurveyStatsItem().getResponse() > 0 ? "true" : "false";
        SurveyItem survey = itemResult.getSurveyItem();
        dic.setVariable("ID_SURVEY", String.format("%s", idSurvey));
        dic.setVariable("TITLE", survey.getTitle());
        dic.setVariable("DESCRIPTION", survey.getDescription());
        dic.setVariable("RESPONSE", responsed);
        if (survey.isOption()) {
            dic.setVariable("CHECK", "checked");
        }
        String date = CommonModel.getDate(survey.getBeginDay());
        dic.setVariable("START_DATE", date);
        date = CommonModel.getDate(survey.getEndDay());
        dic.setVariable("END_DATE", date);
        dic.setVariable("POINT", String.format("%s", survey.getPoint()));
        int index = 1;
        for (Question question : survey.getQuestions()) {
            TemplateDataDictionary sliceQuestions = dic.addSection("SLICE_QUESTION");
            sliceQuestions.setVariable("ID_QUESTION", String.format("%s", question.getIdQues()));
            sliceQuestions.setVariable("INDEX", String.format("%s", index));
            sliceQuestions.setVariable("TITLE", question.getTitleQues());
            sliceQuestions.setVariable("HELP", question.getHelpText());
            if (map.isEmpty()) {
                sliceQuestions.setVariable("EDIT", "false");
            } else {
                Integer isEdit = map.get(question.getIdQues());
                if (isEdit == null) {
                    sliceQuestions.setVariable("EDIT", "false");
                } else {
                    sliceQuestions.setVariable("EDIT", "true");
                }
            }
            if (question.isRequire()) {
                sliceQuestions.setVariable("REQUIRE", "(*)");
            } else {
                sliceQuestions.setVariable("REQUIRE", "");
            }
            sliceQuestions.setVariable("REQUIRE_VALUE", String.format("%s", question.isRequire()));
            if (question.getType() == QuesType.SINGLE_LINE) {
                sliceQuestions.setVariable("ANSWER", CommonModel.getTextAnswer(TemplateDictionary.create()));
            }
            if (question.getType() == QuesType.MULTI_LINE) {
                String content1 = CommonModel.getParagraphAnswer(TemplateDictionary.create());
                sliceQuestions.setVariable("ANSWER", content1);
            }
            if (question.getType() == QuesType.SINGLE_SELECTION) {

                TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                for (String answer : question.getChoices()) {
                    TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                    sliceAnswers.setVariable("OPTION", answer);
                    sliceAnswers.setVariable("RADIO_NAME", String.format("%s", question.getIdQues()));
                }
                content = CommonModel.getSingleChoiceAnswer(dicAnswers);
                sliceQuestions.setVariable("ANSWER", content);

            }

            if (question.getType() == QuesType.MULTI_CHOICE) {

                TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                for (String answer : question.getChoices()) {
                    TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                    sliceAnswers.setVariable("OPTION", answer);
                }
                content = CommonModel.getMultipleChoiceAnswer(dicAnswers);
                sliceQuestions.setVariable("ANSWER", content);

            }

            index++;
        }
        dic.setVariable("BACKLINK", "stat?id=" + idSurvey);
        Template template = RenderModel.getCTemplate("edit_survey");
        content = template.renderToString(dic);

        return content;
    }
}
